/**Additional Information Script**/
function AdditionalInformationPanel(r, listingId)
{	
	var r = JSON.parse(r);
	var htmlCode = r.html;
	var pictureUrlsRaw  = r.pictures;
	this.navBar = { buttons: [], selected : null};
	this.closeFx;
	this.listingId = listingId;
	this.listingEl;
	this.profileEl;
	var thisObj = this;
	var BUTTON_SELECTED_CLASS = "MIP-nav-bar-button-selected";
	var additionalInfoPanel = this;

	function NavBarButton()
	{
		this.buttonEl;
		this.buttonSelected = false;

		this.setSelected = function()
		{
			if(additionalInfoPanel.navBar.selected)
			{
				additionalInfoPanel.navBar.selected.unselect();
			}
			this.buttonEl.classList.add(BUTTON_SELECTED_CLASS);
			additionalInfoPanel.navBar.selected = this;
			this.buttonSelected = true;
		};

		this.unselect = function()
		{
			if(this.buttonSelected)
			{
				this.buttonEl.classList.remove(BUTTON_SELECTED_CLASS);
				this.buttonSelected = false;
				additionalInfoPanel.navBar.selected = null;
			}
			
		};

		this.setOnClick = function(clickFx)
		{
			$(this.buttonEl).on("click", clickFx);
		};
	};
	var initNavBarListing = function()
	{
		var listingButton = new NavBarButton();
		listingButton.buttonEl = document.getElementById("home-button");
		additionalInfoPanel.navBar.buttons.push(listingButton);
		var onClickListingButton = function()
		{
			listingButton.setSelected();
		};
		listingButton.setOnClick(onClickListingButton);

		var picturesButton = new NavBarButton();
		picturesButton.buttonEl = document.getElementById("picture-button");
		additionalInfoPanel.navBar.buttons.push(picturesButton);
		var onClickPicturesButton = function()
		{
			picturesButton.setSelected();
		};
		picturesButton.setOnClick(onClickPicturesButton);

		var contactButton = new NavBarButton();
		contactButton.buttonEl = document.getElementById("contact-button");
		additionalInfoPanel.navBar.buttons.push(contactButton);
		var onClickContactButton = function()
		{
			contactButton.setSelected();
		};
		contactButton.setOnClick(onClickContactButton);

		var closeButton = document.getElementById("exit-button");
		var closeFx = function()
		{
			debugger;
			additionalInfoPanel.closeFx();
		}
		$(closeButton).on("click", closeFx);

		$(listingButton.buttonEl).trigger("click");
	};
	var viewProfile = function()
	{
		var afterFx = function(r)
		{
			debugger;
			var r = JSON.parse(r);
			var html = r.html;
			pictureUrlsRaw = r.pictures;
			thisObj.listingEl = $(".MIP");
			thisObj.listingEl.detach();
			var moreInfoPanel = $("#moreInfoPanel");
			moreInfoPanel.html(html);
			thisObj.profileEl = $(".MIP");
			initNavBarListing();
			var listingLink = $(".listing-link");
			listingLink.on("click", function(){
				debugger;
				var afterListingClick = function(r)
				{
					var r = JSON.parse(r);
					htmlCode = r.html;
					pictureUrlsRaw = r.pictures;
					init();
				}
				$.post(url, {id: listingId}, afterListingClick);
			});
			var controls = {right: $(".profile-image .slider-control .right"), left: $(".profile-image .slider-control .left")};
			var pictureSlider = new PictureSlider($(".profile-image .image-container"), getPictures(pictureUrlsRaw), controls);
		};
		var params = {action: "profile", id: thisObj.listingId};
		var url = getURL("getAdditionalInformation.php");
		$.post(url, params, afterFx);
	}
	var initListingEvents = function()
	{
		var viewProfileLink = $("#view-profile-link");
		$(viewProfileLink).on("click", viewProfile);
	}
	var getPictures = function(pictureUrlsRaw)
	{
		var pictureUrls = [];
		var firstImage = [];
		if(!pictureUrlsRaw || pictureUrlsRaw.length == 0)
		{
			pictureUrls.push({url: "img/no-pic.jpeg"});
			return pictureUrls;
		}
		for(var i=0; i<pictureUrlsRaw.length; i++)
		{
			var imageInfo = pictureUrlsRaw[i];
			var theImageUrl = imageInfo.cropped === "1" ? imageInfo.cropped_picture_url : imageInfo.picture_url;
			var mainPicture = imageInfo.main_picture;
			var theImage = {
				url: theImageUrl
			}
			if(mainPicture == 1)
			{
				firstImage.push(theImage);
			}
			else
			{
				pictureUrls.push(theImage);
			}

		}
		return firstImage.concat(pictureUrls);
	}
	var initListingPanel = function()
	{
		initNavBarListing();
	}
	var init = function()
	{
		debugger;
		$(".module-background").css("background-color", "rgba(0,0,0,0.3)");
		$("#moreInfoPanel > div").not(".module-background").not(".white-layer").remove();
		$("#moreInfoPanel").append(htmlCode);
		$("#moreInfoPanel").css('left',"650px");
		initListingPanel();
		debugger;
		initListingEvents();
		var controls = {right: $(".listing-image .slider-control .right"), left: $(".listing-image .slider-control .left")};
		var pictureSlider = new PictureSlider($(".listing-image .image-container"), getPictures(pictureUrlsRaw), controls);
	}
	init();
}